<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-row>
        <el-col>
      <el-form-item label="商品代码" prop="goodsCode">
        <el-input
          v-model="queryParams.goodsCode"
          placeholder="请输入商品代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库编号" >
        <el-select v-model="queryParams.warehouseCode" clearable  placeholder="请选择仓库编号" :style="{width: '100%'}">
          <el-option
            v-for="w in warehouseList"
            :key="w.id"
            :label="w.warehouseName"
            :value="w.warehouseCode"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="推荐状态">
        <el-select v-model="queryParams.bmSpecificationList[0].isRecommond" placeholder="请选择推荐状态">
          <el-option label="推荐" :value="1"></el-option>
          <el-option label="未推荐" :value="0"></el-option>
        </el-select>
      </el-form-item>
      </el-col>
      </el-row>
      <el-form-item label="一级分类">
        <el-select v-model="selectedFirstCategoryId" placeholder="选择一级分类" :style="{width: '100%'}">
          <el-option
            v-for="c in categoryFirstList"
            :key="c.id"
            :label="c.cname"
            :value="c.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select v-model="selectedSecondCategoryId" placeholder="选择二级分类" :style="{width: '100%'}">
          <el-option
            v-for="c in filteredSecondCategoryList"
            :key="c.id"
            :label="c.cname"
            :value="c.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ruoyi-goods:goods:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['ruoyi-goods:goods:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ruoyi-goods:goods:remove']"
        >批量删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品代码" align="center" prop="goodsCode" :show-overflow-tooltip='true'/>
      <el-table-column label="仓库代码" align="center" prop="warehouseCode" :show-overflow-tooltip='true'/>
      <el-table-column label="商品分类" align="center" prop="categoryName" :show-overflow-tooltip='true'/>
      <el-table-column label="商品名称" align="center" prop="goodsName" :show-overflow-tooltip='true'/>
      <el-table-column label="品牌" align="center" prop="bmBrandList[0].bname" :show-overflow-tooltip='true'/>
      <el-table-column label="规格数量" align="center" prop="count" />
      <el-table-column label="库存" align="center" prop="bmSpecificationList[0].stock" />
      <el-table-column label="是否推荐" align="center" prop="bmSpecificationList[0].isRecommond">
        <template slot-scope="scope">
            <el-tag :type="scope.row.bmSpecificationList[0].isRecommond==1 ? 'success' : 'info'">{{ scope.row.bmSpecificationList[0].isRecommond==1 ? '推荐' : '未推荐' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ruoyi-goods:goods:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ruoyi-goods:goods:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  listGoods,
  getGoods,
  delGoods,
  addGoods,
  updateGoods,
  getWarehouseList,
  getMaxCode
} from "@/api/ruoyi-goods/goods";
import {firstList, getCategory, listCategory, secondList} from "@/api/ruoyi-goods/category";
import {listBName} from "@/api/ruoyi-goods/brand";
import uploadImage from "@/components/ImageUpload/index.vue";

export default {
  name: "Goods",
  components: {uploadImage},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品信息管理表格数据
      goodsList: [{
        categoryName: null,
        bmCategoryList: [],
        bmSpecificationList: [{
          isRecommond: ''
        }],
        bmBrandList: []
      }],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsCode: null,
        goodsName: null,
        warehouseCode: null,
        bmSpecificationList:[{
          isRecommond: '',
        }],
        categoryId: null
      },
      selectedFirstCategoryId: null,
      selectedSecondCategoryId: null,
      // 仓库列表绑定下拉框
      warehouseList:[],
      // 类别列表绑定下拉框
      categoryFirstList:[],
      categoryList:[],
      categoryChildList: [],
      brandList:[],
      oldGoodsList: []
    };
  },
  mounted() {
    this.getList();
    this.getWarehouseNameList();
    this.getCategoryList();
    this.getCategoryFirstNameList();
    this.getCategorySecondNameList();
    this.getBrandList();
  },
  computed: {
    filteredSecondCategoryList() {
      return this.categoryChildList.filter(c => c.parentId === this.selectedFirstCategoryId);
    },
  },
  watch: {
    selectedFirstCategoryId() {
      this.selectedSecondCategoryId = null;
    },
    selectedSecondCategoryId(newValue) {
      this.queryParams.categoryId = newValue;
    },
  },
  methods: {
    insertSpecification(){
      const emptyForm = {
        specificationName: '',
        retailPrice: '',
        stock: '',
        barCode: '',
        qualityPeriod: '',
        productionPlace: '',
        purchaseLimit: '',
        minOrderQuantity: '',
        minOrderPrice: '',
        goodsImgUrl: ''
      };
      this.form.bmSpecificationList.push(emptyForm);
      console.log("<>",this.form);
    },
    /** 查询商品信息管理列表 */
    getList() {
      const warehouseCode = this.$route.params && this.$route.params.warehouseCode;
      this.queryParams.warehouseCode=warehouseCode
      this.loading = true;
      listGoods(this.queryParams).then(response => {
        console.log("queryParams=",this.queryParams);
        console.log("response=",response);
        this.goodsList = response.rows;
        this.total = response.total;
        this.loading = false;
          this.goodsList.forEach(goods => {
            if (goods.bmCategoryList && goods.bmCategoryList.length > 0) {
              const category = goods.bmCategoryList[0];
              if (category.parentId !== 0) {
                getCategory(category.parentId).then(response => {
                  if (response.data.cname !== null) {
                    this.$set(goods, 'categoryName', response.data.cname + "|" + category.cname);
                  }
                });
              }
            }
          });
      });
    },
    getWarehouseNameList(){
      getWarehouseList().then(response => {
        this.warehouseList = response.data;
      })
    },
    getCategoryList() {
      listCategory().then(response => {
        this.categoryList = response.data;
      })
    },
    getBrandList(){
      listBName().then(response => {
        this.brandList = response.data;
      })
    },
    getCategoryFirstNameList(){
      firstList().then(response => {
        this.categoryFirstList = response.data;
      })
    },
    getCategorySecondNameList(){
      secondList().then(response => {
        this.categoryChildList = response.data;
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.bmSpecificationList[0].isRecommond = '';
      this.queryParams.categoryId = null;
      this.queryParams.warehouseCode = null;
      this.selectedFirstCategoryId = null;
      this.selectedSecondCategoryId = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.$router.push({path: "/goods/add/addGoods", query: {data: '新增'}});
      this.$router.push(
        {
          name: "addGoods"
        }
      );
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // this.$router.push({path: "/goods/add/addGoods", query: {data: '修改',id: row.id}});
        this.$router.push(
          {
            name : "addGoods",
            params:row
          }
        );
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商品信息管理编号为"' + ids + '"的数据项？').then(function() {
        return delGoods(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    delSpecification(index){
      this.form.bmSpecificationList.splice(index, 1);
    },
  }
};
</script>
